#!/usr/bin/perl -w
#Importing the manuscript data into the database. Import scripts have the same basename (plus '.pl' extension) as the directory containing the manuscript data

use strict;
use warnings;
use utf8;
#use encoding "utf8", STDIN => 'utf8', STDOUT => 'utf8';
use open ':encoding(utf8)';
use Algorithm::Diff;
use Unicode::Normalize qw(decompose);
use XML::LibXML;
use lib '../';
use ImportHelpers;

my $startTime = time();

open EXTERNALS, "externals.txt"
  or die "externals.txt not found";

if(grep /^(-a|--delete-all)$/, @ARGV){
	ImportHelpers::removeAllWorks();
}

while(<EXTERNALS>){
	chomp;
	next if /^\s*(#|$)/;
	my($dir, $url) = split(/\s+/, $_, 2);
	
	if(!-d $dir){
		warn " - External directory '$dir' doesn't exist.\n";
		next;
	}
	if(!-f "$dir.pl"){
		warn " - Import script called '$dir.pl' doesn't exist\n";
		next;
	}
	
	print "# Running import script $dir.pl\n";
	require "$dir.pl";
	#print "$dir == $url\n";
}

if(!grep /^(-s|--no-merge|--skip-merge)$/, @ARGV){
	require "merge.pl";
}

printf("\nImport finished. Elapsed time: %.2f mins\n",  ((time()-$startTime)/60));

1;